﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
using WeiBoUI.Helper;

namespace WeiBoUI.ScrollEx
{
    public class KHScrollBar : UserControl
    {
        private Image leftImg = ImageHelper.GetResBitmap("ScrollBarImg.HScroll_Left.png");
        private Image middleImg = ImageHelper.GetResBitmap("ScrollBarImg.HScroll_Middle.png");
        private Image rightImg = ImageHelper.GetResBitmap("ScrollBarImg.HScroll_Right.png");
        private Color d = Color.FromArgb(235, 243, 246);
        private Color e = Color.FromArgb(220, 230, 240);
        private int f = 100;
        private int g = 10;
        private int h;
        private bool i;
        private int j = 0;
        private int k;
        private EventHandler l = null;
        private IContainer m = null;
        public event EventHandler ValueChange
        {
            [MethodImpl(MethodImplOptions.Synchronized)]
            add
            {
                this.l = (EventHandler)Delegate.Combine(this.l, value);
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            remove
            {
                this.l = (EventHandler)Delegate.Remove(this.l, value);
            }
        }
        public Image ImageLeftArrow
        {
            get
            {
                return this.leftImg;
            }
            set
            {
                this.leftImg = value;
            }
        }
        public Image ImageThumb
        {
            get
            {
                return this.middleImg;
            }
            set
            {
                this.middleImg = value;
            }
        }
        public Image ImageRightArrow
        {
            get
            {
                return this.rightImg;
            }
            set
            {
                this.rightImg = value;
            }
        }
        [Description("通道的背景色")]
        public Color ColorBg
        {
            get
            {
                return this.d;
            }
            set
            {
                this.d = value;
            }
        }
        [Description("通道的边框颜色")]
        public Color ColorLine
        {
            get
            {
                return this.e;
            }
            set
            {
                this.e = value;
            }
        }
        public int Maximum
        {
            get
            {
                return this.f;
            }
            set
            {
                this.f = value;
            }
        }
        public int ThumbWidth
        {
            get
            {
                return this.g;
            }
            set
            {
                this.g = value;
            }
        }
        public int Value
        {
            get
            {
                return this.h;
            }
            set
            {
                this.h = value;
            }
        }
        public override bool AutoSize
        {
            get
            {
                return base.AutoSize;
            }
            set
            {
                base.AutoSize = value;
                if (this.AutoSize)
                {
                    base.Height = this.leftImg.Height;
                }
            }
        }
        public KHScrollBar()
        {
            this.Init();
            base.SetStyle(ControlStyles.UserPaint, true);
            base.SetStyle(ControlStyles.DoubleBuffer, true);
            base.SetStyle(ControlStyles.ResizeRedraw, true);
            base.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            base.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.AutoSize = true;
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics graphics = e.Graphics;
            graphics.SmoothingMode = SmoothingMode.AntiAlias;
            graphics.DrawImage(this.leftImg, 0, 0, this.leftImg.Width, base.Height);
            Brush brush = new SolidBrush(this.d);
            Pen pen = new Pen(this.e);
            graphics.FillRectangle(brush, new Rectangle(this.leftImg.Width, 0, base.Width - this.leftImg.Width - this.rightImg.Width - 1, base.Height));
            graphics.DrawLine(pen, this.leftImg.Width, 0, base.Width - this.rightImg.Width - 1, 0);
            graphics.DrawLine(pen, this.leftImg.Width, base.Height - 1, base.Width - this.leftImg.Width - 1, base.Height - 1);
            int num = base.Width - this.leftImg.Width - this.rightImg.Width;
            float num2 = (float)this.g / (float)this.f * (float)num;
            int num3 = (int)num2;
            if (num3 > num)
            {
                num3 = num;
                num2 = (float)num;
            }
            if (num3 < 14)
            {
                num3 = 14;
            }
            int num4 = 5;
            int num5 = this.j;
            num5 += this.leftImg.Width;
            graphics.DrawImage(this.middleImg, new Rectangle(num5, 0, num4, base.Height), new Rectangle(0, 0, num4, this.middleImg.Height), GraphicsUnit.Pixel);
            num5 += num4;
            graphics.DrawImage(this.middleImg, new Rectangle(num5, 0, num3 - 2 * num4, base.Height), new Rectangle(num4, 0, this.middleImg.Width - 2 * num4, this.middleImg.Height), GraphicsUnit.Pixel);
            num5 += num3 - 2 * num4;
            graphics.DrawImage(this.middleImg, new Rectangle(num5, 0, num4, base.Height), new Rectangle(this.middleImg.Width - num4, 0, num4, this.middleImg.Height), GraphicsUnit.Pixel);
            graphics.DrawImage(this.rightImg, base.Width - this.rightImg.Width, 0, this.rightImg.Width, base.Height);
        }
        private void MouseDownAction(object sender, MouseEventArgs args)
        {
            Point pt = base.PointToClient(Cursor.Position);
            int num = base.Width - this.leftImg.Width - this.rightImg.Width;
            float num2 = (float)this.g / (float)this.f * (float)num;
            int num3 = (int)num2;
            if (num3 > num)
            {
                num3 = num;
                num2 = (float)num;
            }
            if (num3 < 14)
            {
                num3 = 14;
            }
            int num4 = this.j;
            num4 += this.leftImg.Width;
            Rectangle rectangle = new Rectangle(num4, 0, num3, base.Height);
            if (rectangle.Contains(pt))
            {
                this.i = true;
                this.k = args.X - num4;
            }
        }
        private void MouseMoveAction(object A_0, MouseEventArgs A_1)
        {
            if (this.i)
            {
                int num = base.Width - this.leftImg.Width - this.rightImg.Width;
                float num2 = (float)this.g / (float)this.f * (float)num;
                int num3 = (int)num2;
                if (num3 > num)
                {
                    num3 = num;
                    num2 = (float)num;
                }
                if (num3 < 14)
                {
                    num3 = 14;
                }
                this.j = A_1.X - this.k - this.leftImg.Width;
                if (this.j <= 0)
                {
                    this.j = 0;
                }
                if (this.j >= num - num3)
                {
                    this.j = num - num3;
                }
                float num4 = (float)this.j / (float)(base.Width - this.leftImg.Width - this.rightImg.Width - num3) * (float)(this.Maximum - this.g);
                this.h = (int)num4;
                if (this.l != null)
                {
                    this.l(this, new EventArgs());
                }
                base.Invalidate();
            }
        }
        private void MouseUpAction(object A_0, MouseEventArgs A_1)
        {
            this.i = false;
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing && this.m != null)
            {
                this.m.Dispose();
            }
            base.Dispose(disposing);
        }
        private void Init()
        {
            base.SuspendLayout();
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Name = "LMHScrollBar";
            base.MouseMove += new MouseEventHandler(this.MouseMoveAction);
            base.MouseDown += new MouseEventHandler(this.MouseDownAction);
            base.MouseUp += new MouseEventHandler(this.MouseUpAction);
            base.ResumeLayout(false);
        }
    }
}
